package com.zd.lib.http.retrofit;


import com.zd.lib.App;
import com.zd.lib.BuildConfig;
import com.zd.lib.demo.TestInterceptor;
import com.zd.lib.http.gson.Json;
import com.zd.lib.http.okhttp.CommonRequestInterceptor;
import com.zd.lib.http.okhttp.OKHttpUtils;
import com.zd.lib.http.okhttp.PostNoNetWorkCacheInterceptor;
import com.zd.lib.http.response.BaseEntity;
import com.zd.lib.http.retrofit.utils.ErrorCheckerCompat;
import com.zd.lib.http.retrofit.utils.ErrorCheckerTransformer;
import com.zd.lib.http.retrofit.utils.SchedulerCompat;
import com.zd.lib.utils.FileUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
 * Retrofit + RxJAVA  的封装
 *
 * @author zoudong
 * @version 2.0   加入缓存
 */
public class RetrofitUtils {

    public static Retrofit retrofit;
    public static OkHttpClient.Builder builder;
    private static RetrofitUtils simple;
    private final OKHttpUtils defaultOkHttp = new OKHttpUtils();
    public static final int CACHESIZE=100 * 1024 * 1024;
    public static final String NETCACHE = "netcache";
    private RetrofitUtils() {
        builder = defaultOkHttp.createDefaultOKHttp();
        // 缓存的路径 和缓存总的大小
        builder.addInterceptor(new PostNoNetWorkCacheInterceptor(getCacheFile(),CACHESIZE));
        builder.addInterceptor(new CommonRequestInterceptor());  //统一接口参数
        builder.addInterceptor(new TestInterceptor());
        builder.addNetworkInterceptor(new TestInterceptor());
    }

    /**
     * 获取缓存路径
     */
    public File getCacheFile() {
        return new File(FileUtils.getCacheDir(App.getInstance()) + NETCACHE);
    }
    public static RetrofitUtils SIGNLE() {
        if (simple == null) {
            synchronized (RetrofitUtils.class) {
                simple = new RetrofitUtils();
            }
        }

        return simple;
    }

    public RetrofitUtils build(String baseUrl) {
      return   build(baseUrl, null);
    }

    public RetrofitUtils build(String baseUrl, OkHttpClient okHttpClient) {
        if (okHttpClient == null) {
            okHttpClient = builder.build();
        }
        if (retrofit == null) {
            retrofit = getDefaultRetrofit(baseUrl, okHttpClient);
        }
        return  this;
    }

    public static Retrofit getDefaultRetrofit(String baseUrl, OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(Json.create()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient).build();
        return retrofit;
    }

    public RetrofitUtils build(Retrofit retro) {
        retrofit = retro;
        return  this;
    }

    /**
     * 创建接口
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T create(Class<T> clazz) {
        return retrofit.create(clazz);
    }

    /**
     * 解析 code==0
     * @param <T>
     * @return
     */
    public  static  <T> Observable.Transformer<BaseEntity<T>,BaseEntity<T>> wrapperRetrofit() {
        return new Observable.Transformer<BaseEntity<T>, BaseEntity<T>>() {
            @Override
            public Observable<BaseEntity<T>> call(Observable<BaseEntity<T>> observable) {
                return observable.flatMap(new ErrorCheckerCompat<T>())
                        .compose(SchedulerCompat.<BaseEntity<T>>applyIoSchedulers());
            }

        };
    }
    public  static  <T> Observable.Transformer<BaseEntity<T>,T> wrapperRetrofit2() {
        return new Observable.Transformer<BaseEntity<T>, T>() {
            @Override
            public Observable<T> call(Observable<BaseEntity<T>> observable) {
                return observable.compose(new ErrorCheckerTransformer<BaseEntity<T>, T>())
                        .compose(SchedulerCompat.<T>applyIoSchedulers());
            }

        };
    }
}
